using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Serialcoder.Erp.Tools.PatchCreator
{
	public partial class BrowsePatchForm : Form
	{
		public BrowsePatchForm()
		{
			InitializeComponent();
		}
        //MainForm m_ParentForm = null;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            uxListView.Items.Clear();
            uxListView.Enabled = false;

            //m_ParentForm = (this.MdiParent) as Form1;

            //m_ParentForm.uxToolStripProgressBar.Visible = true;
            //m_ParentForm.uxToolStripProgressBar.Style = ProgressBarStyle.Marquee;


            uxBackgroundWorker.RunWorkerAsync();
        }

        private void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {

            //SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString);
            //connection.Open();

            //SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            //command.CommandText = "select * from SchemaPatch order by id desc";

            //e.Result = command.ExecuteReader(CommandBehavior.CloseConnection);
            e.Result = Runtime.DAL.GetAll();
        }

        private void OnBackgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            List<Runtime.Patch> reader = (List<Runtime.Patch>)e.Result;

            uxListView.BeginUpdate();
            uxListView.Enabled = true;
            //uxAcceptButton.Enabled = true;
            //m_ParentForm.uxToolStripProgressBar.Visible = false;

            foreach (Runtime.Patch patch in reader)
            {
                ListViewItem item = new ListViewItem(patch.Id.ToString());
                item.SubItems.Add(patch.Author);
                item.SubItems.Add(patch.CreationDate.ToString("dd/MM/yyyy"));
                item.SubItems.Add(patch.Description);
                item.Tag = patch;
                uxListView.Items.Add(item);
            }

            //while (reader.Read())
            //{
            //    ListViewItem item = new ListViewItem(reader["id"].ToString());
            //    item.SubItems.Add(reader["createdBy"].ToString());
            //    item.SubItems.Add(reader["CreationDate"].ToString());
            //    item.SubItems.Add(reader["Description"].ToString());
            //    item.Tag = reader["sql"];
            //    uxListView.Items.Add(item);
            //}

            //reader.Close();

            uxListView.EndUpdate();
        }

        private void uxOpenButton_Click(object sender, EventArgs e)
        {
            if (uxListView.SelectedItems.Count == 1)
            {
                m_Patch = uxListView.SelectedItems[0].Tag as Runtime.Patch;
                DialogResult = DialogResult.OK;
            }
            else
            {
                m_Patch = null;
            }
        }

        private Runtime.Patch m_Patch;

        public Runtime.Patch Patch
        {
            get { return m_Patch; }
            set { m_Patch = value; }
        }

        private void uxListView_DoubleClick(object sender, EventArgs e)
        {
            uxOpenButton_Click(sender, e);
        }

	}
}